{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-*- coding: utf-8 -*-"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 条件测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#比较运算\n",
    "a = 10\n",
    "b = 8\n",
    "print (a>b) #大于\n",
    "print (a<b) #小于\n",
    "print (a >= b)#大于等于\n",
    "print (a <= b)#小于等于\n",
    "print (a==b)\n",
    "print (a != b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "非空\n"
     ]
    }
   ],
   "source": [
    "#非空判断\n",
    "ls = [1]\n",
    "if ls:   #数据结构不为空、变量不为0、None、False 则条件成立\n",
    "    print(\"非空\")\n",
    "else:\n",
    "    print(\"空的\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#逻辑运算 与 或 非\n",
    "a = 10\n",
    "b = 8\n",
    "c = 12\n",
    "print ((a > b)and(b > c)) #与\n",
    "print ((a > b)or(b > c)) #或\n",
    "print (not(a > b)) #非"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#复合逻辑运算的优先级 非 >  与 > 或\n",
    "print (True or True and False)\n",
    "print ((True or True) and False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#存在运算\n",
    "#元素in 列表/字符串\n",
    "cars = [\"BYD\",\"BMW\",\"AUDI\",\"TOYOTA\"]\n",
    "print (\"BMW\" in cars)\n",
    "print (\"BENZ\" in cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#元素 not in 列表/字符串\n",
    "print (\"BMW\" not in cars)\n",
    "print (\"BENZ\" not in cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分支结构 if语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "孩子，你该上学了\n"
     ]
    }
   ],
   "source": [
    "#单分支\n",
    "age = 8\n",
    "if age > 7:\n",
    "    print (\"孩子，你该上学了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "再等等\n"
     ]
    }
   ],
   "source": [
    "#双分支\n",
    "age = 18\n",
    "if age > 22:\n",
    "    print (\"可以结婚啦\")\n",
    "else:\n",
    "    print (\"再等等\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大家都要工作\n"
     ]
    }
   ],
   "source": [
    "#多分支\n",
    "age = 38\n",
    "if age < 7:\n",
    "    print (\"再玩两年泥巴\")\n",
    "elif age < 13:\n",
    "    print (\"孩子，你该上初中了\")\n",
    "elif age < 60:\n",
    "    print (\"大家都要工作\")\n",
    "else:\n",
    "    print (\"退休了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for 循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Congratulation,李雷\n",
      "Congratulation,韩梅梅\n",
      "Congratulation,Jim\n"
     ]
    }
   ],
   "source": [
    "# 循环流程 从可迭代对象中，每次取出一个元素，并进行相应对操作\n",
    "#直接迭代 -- 列表[],元组(),集合{},字符串\" \"\n",
    "graduates = (\"李雷\",\"韩梅梅\",\"Jim\")\n",
    "for graduate in graduates:\n",
    "    print (\"Congratulation,\"+graduate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201901 xiaoming\n",
      "201902 xiaohong\n",
      "201903 xiaoqiang\n",
      "201901\n",
      "201902\n",
      "201903\n"
     ]
    }
   ],
   "source": [
    "#变换迭代 -- 字典\n",
    "students = {201901:'xiaoming',201902:'xiaohong',201903:'xiaoqiang'}\n",
    "for k,v in students.items():\n",
    "    print (k,v)\n",
    "for student in students.keys():\n",
    "    print (student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16]\n",
      "[1, 9, 25, 49, 81]\n"
     ]
    }
   ],
   "source": [
    "#range()对象\n",
    "res = []\n",
    "for i in range(1000):\n",
    "    res.append(i**2)\n",
    "print (res[:5])\n",
    "\n",
    "res = []\n",
    "for i in range(1,10,2):\n",
    "    res.append(i**2)\n",
    "print (res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "产品抽检不合格\n"
     ]
    }
   ],
   "source": [
    "#循环控制  break结束整个循环\n",
    "product_scores = [89,90,99,70,67]\n",
    "i = 0\n",
    "for score in product_scores:\n",
    "    if score < 75:\n",
    "        i += 1\n",
    "    if i == 2:\n",
    "        print (\"产品抽检不合格\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第3个产品，分数为70,不合格\n",
      "第4个产品，分数为67,不合格\n"
     ]
    }
   ],
   "source": [
    "#continue 结束本次循环\n",
    "product_scores = [89,90,99,70,67]\n",
    "for i in range(len(product_scores)):\n",
    "    if product_scores[i] >= 75:\n",
    "        continue\n",
    "    print (\"第{0}个产品，分数为{1},不合格\".format(i,product_scores[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "产品抽检不合格\n"
     ]
    }
   ],
   "source": [
    "#for与else的配合\n",
    "#如果for循环全部执行完毕，没有被break中止，则运行else块\n",
    "product_scores = [89,90,99,70,67]\n",
    "#如果低于75分的超过2个，则该组产品不合格\n",
    "i = 0\n",
    "for score in product_scores:\n",
    "    if score < 75:\n",
    "        i += 1\n",
    "    if i == 2:\n",
    "        print (\"产品抽检不合格\")\n",
    "        break\n",
    "else:\n",
    "    print(\"产品抽检不合格\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 无限循环  while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "res = 0\n",
    "while i < 5:\n",
    "    res += i\n",
    "    i += 1\n",
    "print (res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}